<template>
  <div id="contentBody" class="box-body">
    <div class="box box-primary">
      <div class="box-header with-border">
        <h3 class="box-title">家庭及主要社会关系</h3>
        <div class="box-tools pull-right">
          <button type="button" v-if='isEditable' class="btn btn btn-success" @click="editVisiable = true">
              <i class='fa fa-plus-square-o'>添加社会关系</i>
            </button>
        </div>
      </div>
      <div class="box-body">
        <div class=" form-inline dt-bootstrap" id="example1_wrapper">
          <div class="row">
            <div class="col-sm-12 table-responsive">
              <table class="table table-hover table table-bordered table-striped dataTable">
                <tbody>
                  <tr>
                    <th aria-label="称谓" style="width: 142px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">称谓</th>
                    <th aria-label="姓名" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">姓名</th>
                    <th aria-label="出生年月" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">出生年月</th>
                    <th aria-label="政治面貌" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">政治面貌</th>
                    <th aria-label="工作单位（就读学校）" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">工作单位（就读学校）</th>
                    <th aria-label="现任职务" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">现任职务</th>
                    <th aria-label="目前状态" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">目前状态</th>
                    <th aria-label="所属系统" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">所属系统</th>
                    <th aria-label="备注" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">备注</th>
                    <th v-if="isEditable" aria-label="操作" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">操作</th>
                  </tr>
                  <tr v-for='(value,index) in this.relations' role="row" class="value.cls" v-bind:key="index">
                    <td> {{ value.title }}</td>
                    <td>{{ value.name }}</td>
                    <td>{{ value.birthday}}</td>
                    <td class="text-light" v-text="getpolicyname(value.policy)"></td>
                    <td>{{ value.company }}</td>
                    <td>{{ value.position }}</td>
                    <td class="text-light" v-text="getworkstatusname(value.workstatus)"></td>
                    <td>{{ value.system }}</td>
                    <td>{{ value.comment }}</td>
                    <td v-if="isEditable">
                      <div class="col-md-2">
                        <button @click="deletesocial(index)" class="btn btn-xs">删除</button>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
      <el-dialog title="新增社会关系" :visible.sync="editVisiable" :modal="false" size="small">
        <div class="form-horizontal">
          <div class="box-body">
            <div class="form-group">
              <label class="col-md-2 control-label my-label">称谓</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editsocial.title" placeholder="请输入称谓">
              </div>
              <label class="col-md-2 control-label my-label">姓名</label>
              <div class="col-sm-4">
                <input class="form-control" v-model='editsocial.name' placeholder="请输入姓名">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">出生年月</label>
              <div class="col-sm-4">
                <MyDatePicker v-bind:date="editsocial.birthday" @timeChanged="time=>editsocial.birthday = time"></MyDatePicker>
              </div>
              <label class="col-md-2 control-label my-label">政治面貌</label>
              <div class="col-sm-4">
                <Policy ref='socialpolicy' :selectedPolicy='editsocial.policy' @policychange="val => editsocial.policy = val"></Policy>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">工作单位（就读学校）</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editsocial.company" placeholder="请输入工作单位">
              </div>
              <label class="col-md-2 control-label my-label">现任职务</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editsocial.position" placeholder="请输入现任职务">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">目前状态</label>
              <div class="col-sm-4">
                <select v-model="editsocial.workstatus" class="form-control">
                    <option :key="option.id" v-for="option in this.workstatuses" v-bind:value="option.id">{{ option.name }}</option>
                  </select>
              </div>
              <label class="col-md-2 control-label my-label">所属系统</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editsocial.system" placeholder="请输入所属系统">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">备注</label>
              <div class="col-sm-9">
                <input class="form-control" v-model="editsocial.comment" placeholder="请输入备注">
              </div>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="editVisiable = false">关 闭</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
          </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import Policy from '../../mycomponents/common/Policy'
  import MyDatePicker from '../../mycomponents/common/MyDatePicker'
  import Utils from '../../../common/utils'
  export default {
    name: 'Social',
    props: ['relations', 'editable'],
    data: function() {
      return {
        isEditable: false,
        editVisiable: false,
        editsocial: {
          title: '',
          name: '',
          birthday: '',
          policy: 1,
          company: '',
          position: '',
          workstatus: 1,
          system: '',
          comment: ''
        },
        workstatuses: []
      }
    },
    components: {
      Policy,
      MyDatePicker
    },
    created() {
      if (this.editable != null) {
        this.isEditable = this.editable
      }
      this.workstatuses = this.$store.state.sysconfigs.workstatuses
      this.utils = new Utils()
    },
    methods: {
      getpolicyname: function(id) {
        console.log('getpolicyname')
        return this.utils.getNameById(id, this.$store.state.sysconfigs.politicalStatus)
      },
      getworkstatusname: function(id) {
        for (var index in this.workstatuses) {
          if (this.workstatuses[index].id == id) {
            return this.workstatuses[index].name;
          }
        }
        return ''
      },
      getData() {
        return this.relations
      },
      clearEditData: function() {
        this.editsocial.title = ''
        this.editsocial.name = '',
        this.editsocial.birthday = '',
        this.editsocial.policy = 1,
        this.editsocial.company = '',
        this.editsocial.position = '',
        this.editsocial.workstatus = 1,
        this.editsocial.system = '',
        this.editsocial.comment = ''
      },
      deletesocial: function(index) {
        this.relations.splice(index, 1)
      },
      deepCopy(o, c) {
        var c = c || {}
        for (var i in o) {
          if (typeof o[i] === 'object') {
            if (o[i].constructor === Array) {
              c[i] = []
            } else {
              c[i] = {}
            }
            this.deepCopy(o[i], c[i])
          } else {
            c[i] = o[i]
          }
        }
        return c
      },
      submit(e) {
        var newsocial = {}
        this.deepCopy(this.editsocial, newsocial)
        this.relations.push(newsocial)
        this.clearEditData()
        this.editVisiable = false
      }
    }
  }
</script>

<style scoped>
  #contentBody {
    min-height: 400px;
  }
</style>